package com.yikes.srb.controller.web;

import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.leadingModle.form.borrower.*;
import com.yikes.srb.leadingModle.vo.BorrowInfoVO;
import com.yikes.srb.leadingModle.vo.BorrowerDetailVO;
import com.yikes.srb.leadingModle.vo.BorrowerVO;
import com.yikes.srb.mybatis.base.PageForm;
import com.yikes.srb.service.BorrowerService;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@RequiredArgsConstructor
@Tag(name = "BorrowerController", description = "借款人管理")
@RestController
@RequestMapping("/admin/borrower")
public class BorrowerController {

    private final BorrowerService service;


    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<BorrowerVO>> listPage(BorrowerQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<BorrowerVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody BorrowerAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody BorrowerEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "保存借款人信息")
    @PostMapping("/auth/save")
    public Result save(@RequestBody BorrowerForm form, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        service.saveBorrowerVOByUserId(form, userId);
        return Result.ok();
    }

    @Operation(description = "获取借款人认证状态")
    @GetMapping("/auth/getBorrowerStatus")
    public Result getBorrowerStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        Integer status = service.getStatusByUserId(userId);
        return Result.ok(status);
    }

    @Operation(description = "获取借款人信息")
    @GetMapping("/show/{id}")
    public Result show(@PathVariable Long id) {

        BorrowerDetailVO borrowerDetailVO = service.getBorrowerDetailVOById(id);
        return Result.ok(borrowerDetailVO);
    }

    @Operation(description = "借款额度审批")
    @PostMapping("/approval")
    public Result approval(@RequestBody BorrowerApprovalForm form) {

        service.approval(form);
        return Result.ok("审批完成");
    }

    @Operation(description = "借款信息列表")
    @GetMapping("/list")
    public Result list(String keyword, PageForm page) {
        PageResult<BorrowInfoVO> borrowInfoList = service.selectList(keyword, page);
        return Result.ok(borrowInfoList);
    }

}
